package fuck.dialog;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import fuck.ActionContext;
import fuck.handler.CreateJavaFilesHandler;

public class CreateJavaFileDialog extends Dialog {
	protected Object result;
	protected Shell shlCreateJavaFiles;
	private ActionContext context = new ActionContext();
	private Button cb_message;
	private Button cb_handler;
	private Button cb_bean;
	private Group group;

	public CreateJavaFileDialog(Shell parent, int style) {
		super(parent, style);
		setText("Java Files Dialog");
	}

	public CreateJavaFileDialog(ActionContext context, int style) {
		this(context.getShell(), style);
		this.context = context;
	}

	public Object open() {
		createContents();
		Display display = getParent().getDisplay();

		this.shlCreateJavaFiles.setLocation(display.getClientArea().width / 2 - this.shlCreateJavaFiles.getSize().x / 2, display.getClientArea().height / 2
				- this.shlCreateJavaFiles.getSize().y / 2);
		this.shlCreateJavaFiles.open();
		this.shlCreateJavaFiles.layout();

		while (!this.shlCreateJavaFiles.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return this.result;
	}

	private void createContents()
  {
    this.shlCreateJavaFiles = new Shell(getParent(), getStyle());
    this.shlCreateJavaFiles.setSize(450, 300);
    this.shlCreateJavaFiles.setText("Create Java Files");

    Label lb_title = new Label(this.shlCreateJavaFiles, 0);
    lb_title.setBounds(27, 30, 62, 20);
    lb_title.setText("数据目录");

    this.group = new Group(this.shlCreateJavaFiles, 0);
    this.group.setText("创建选择");
    this.group.setBounds(27, 63, 361, 123);

    this.cb_message = new Button(this.group, 32);
    this.cb_message.setBounds(10, 23, 242, 16);
    this.cb_message.setText("数据Dao");

    this.cb_handler = new Button(this.group, 32);
    this.cb_handler.setBounds(10, 59, 242, 16);
    this.cb_handler.setText("数据Container");

    this.cb_bean = new Button(this.group, 32);
    this.cb_bean.setBounds(10, 95, 242, 16);
    this.cb_bean.setText("数据Bean");

    Label lb_file = new Label(this.shlCreateJavaFiles, 0);
    lb_file.setBounds(89, 30, 235, 20);
    lb_file.setText(this.context.getConfig().getJavaExcelPackage());

    Button btn_ok = new Button(this.shlCreateJavaFiles, 0);
    btn_ok.addSelectionListener(new SelectionAdapter(){
    	@Override
    	public void widgetSelected(SelectionEvent e) {
    		CreateJavaFilesHandler handler = new CreateJavaFilesHandler(context);
    	    handler.action(new Object[] { Boolean.valueOf(cb_bean.getSelection()), Boolean.valueOf(cb_message.getSelection()), Boolean.valueOf(cb_handler.getSelection()) });
    	    shlCreateJavaFiles.close();
    	}
    });

    btn_ok.setBounds(110, 202, 72, 22);
    btn_ok.setText("确定");

    Button btn_cancel = new Button(this.shlCreateJavaFiles, 0);
    btn_cancel.addSelectionListener(new SelectionAdapter(){
    	@Override
    	public void widgetSelected(SelectionEvent e) {
    		shlCreateJavaFiles.close();
    	}
    });

    btn_cancel.setBounds(263, 202, 72, 22);
    btn_cancel.setText("取消");
  }
}
